/***************************************************************************
 *   Copyright (C) 2008/2009 by Philipp Müller                             *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QtCore>

#include "qtvomp.h"
#include "widgetoptions.h"


WidgetOptions::WidgetOptions( MainWindow* mainWindow, QWidget *parent ) : QWidget( parent ),
    m_mainWindow( mainWindow )
{
    m_ui.setupUi( this );

    m_ui.displayChannels->addItems( QStringList() << tr( "All" ) << tr( "FTA only" ) );
    m_ui.aspectRatio->addItems( QStringList() << tr( "Auto" ) << tr( "Window size" ) << "4:3" << "16:9"  );
    m_ui.scaleMode->addItems( QStringList() << tr( "Fit in view" ) << tr( "Scale and crop" )  );

    m_ui.backButton->setIcon( style()->standardIcon( QStyle::SP_ArrowLeft ) );
    connect( m_ui.backButton, SIGNAL( clicked() ), this, SLOT( backClicked() ) );
}


WidgetOptions::~WidgetOptions()
{
}


void WidgetOptions::backClicked()
{
    saveOptions();

    emit back();
}


void WidgetOptions::loadOptions()
{
    if( !m_mainWindow->vdr() )
    {
        return;
    }

    //Display Channels
    m_displayChannel = m_mainWindow->vdr()->loadConfig( "General", "Channels" );

//     qDebug() << "WidgetOptions::loadOptions: displayChannels is now: " << m_displayChannel;

    if ( m_displayChannel.isEmpty() )
    {
        m_displayChannel = "All";
    }

    if ( m_displayChannel == "All" )
    {
        m_displayChannelOrig = 0;
    }
    else
    {
        m_displayChannelOrig = 1;
    }
    m_ui.displayChannels->setCurrentIndex( m_displayChannelOrig );

    //Aspect ratio
    m_aspectRatioOrig = m_mainWindow->settings()->aspectRatio;
    m_ui.aspectRatio->setCurrentIndex( m_aspectRatioOrig );

    //Scale mode
    m_scaleModeOrig = m_mainWindow->settings()->scaleMode;
    m_ui.scaleMode->setCurrentIndex( m_scaleModeOrig );
}


void WidgetOptions::saveOptions()
{
    if( !m_mainWindow->vdr() )
    {
        return;
    }

    //Display channels
    if( m_ui.displayChannels->currentIndex() != m_displayChannelOrig )
    {
        if( m_ui.displayChannels->currentIndex() == 0 )
        {
            m_mainWindow->vdr()->saveConfig( "General", "Channels", "All" );
        }
        else
        {
            m_mainWindow->vdr()->saveConfig( "General", "Channels", "FTA only" );
        }
        qDebug() << "WidgetOptions::saveOptions: display channels is changed in config on server";
    }

    if( m_ui.aspectRatio->currentIndex() != m_mainWindow->settings()->aspectRatio )
    {
        m_mainWindow->settings()->aspectRatio = (Phonon::VideoWidget::AspectRatio)m_ui.aspectRatio->currentIndex();
        m_mainWindow->settings()->saveAspectRatio();
        qDebug() << "WidgetOptions::saveOptions: aspect ratio is changed in config on server";
    }

    if( m_ui.scaleMode->currentIndex() != m_mainWindow->settings()->scaleMode )
    {
        m_mainWindow->settings()->scaleMode = (Phonon::VideoWidget::ScaleMode)m_ui.scaleMode->currentIndex();
        m_mainWindow->settings()->saveScaleMode();
        qDebug() << "WidgetOptions::saveOptions: scale mode is changed in config on server:";
    }
}
